/*
 * bsp_aes.c
 *
 *  Created on: 2023年7月24日
 *      Author: fjx
 */
#include <bsp_aes.h>

#define xCONFIG_LOG_DS_AES

#if defined (CONFIG_LOG_DS_AES)
#include "app_log.h"
#endif

static psa_key_id_t key_id;
//static psa_key_attributes_t key_attr;



psa_key_id_t DS_aes_get_key_id (void)
{
#if defined (CONFIG_LOG_DS_AES)
  DS_app_log_info("Aes key_id = 0x%0x \n",key_id);
#endif
  return key_id;
}
void DS_aes_destroy_keyid (uint32_t _key_id)
{
  psa_destroy_key(_key_id);
}

DS_AES_STATUS DS_aes_init (uint8_t *key, size_t key_len )
{
  psa_status_t ret;

  if(DS_aes_get_key_id() == AES_KEY_ID ||
      DS_aes_get_key_id() != 0){
      DS_aes_destroy_keyid(key_id);
  }
  psa_key_attributes_t key_attr;
//  psa_cipher_operation_t cipher_op;
  ret = psa_crypto_init();
#if defined (CONFIG_LOG_DS_AES)
  DS_app_log_info("ret = %d,@%s %d\n",ret,__FILE__,__LINE__);
#endif
  // Set up attributes for a AES ECB key
  key_attr = psa_key_attributes_init();
  psa_set_key_type(&key_attr, PSA_KEY_TYPE_AES);
  psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
  psa_set_key_algorithm(&key_attr, PSA_ALG_ECB_NO_PADDING);

  // Import a volatile plain key for AES ECB
  ret = psa_import_key(&key_attr, key, key_len, &key_id);
#if defined (CONFIG_LOG_DS_AES)
  DS_app_log_info("ret = %d\n",ret);
#endif

  return ret;
}


DS_AES_STATUS DS_aes_use_keyid_ae(psa_key_id_t keyid,
                           const uint8_t *plaintext,
                           size_t plaintext_length,
                           uint8_t *ciphertext,
                           size_t ciphertext_size,
                           size_t *ciphertext_length )
{
  DS_AES_STATUS ret = DS_AES_STATUS_SUCCESS;


  ret = psa_cipher_encrypt(keyid, PSA_ALG_ECB_NO_PADDING,
                           plaintext, plaintext_length,
                           ciphertext, ciphertext_size,
                           ciphertext_length);
  return ret;
}
DS_AES_STATUS DS_aes_use_keyid_de(psa_key_id_t keyid,
                           uint8_t *ciphertext,
                           size_t ciphertext_size,
                           uint8_t *plaintext,
                           size_t plaintext_length,
                           size_t *ciphertext_length )
{
  DS_AES_STATUS ret = DS_AES_STATUS_SUCCESS;

  ret = psa_cipher_decrypt(keyid, PSA_ALG_ECB_NO_PADDING,
                           ciphertext, ciphertext_size,
                           plaintext, plaintext_length,
                           ciphertext_length);
  return ret;
}

//void ecb_test (void)
//{
//  uint8_t aes_ecb_key[] = {
//   0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
//   };
//   uint8_t plain_msg_buf[] = {
//   0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
//   };
//   uint8_t buf[16];
//   uint8_t cipher_buf[16];
//   size_t out_len;
//
//   //2.
//   DS_AES_STATUS ret;
//
//   DS_aes_init(aes_ecb_key,sizeof(aes_ecb_key));
//   ret = DS_aes_use_keyid_ae(DS_aes_get_key_id(),
//                       plain_msg_buf,sizeof(plain_msg_buf),
//                       cipher_buf, sizeof(cipher_buf), &out_len);
//   printf("ret = %d,out_len=%d\n",ret,out_len);
//   ret = DS_aes_use_keyid_de(DS_aes_get_key_id(),
//                       cipher_buf, out_len,
//                       buf, sizeof(buf), &out_len);
//   printf("ret = %d,out_len=%d\n",ret,out_len);
//
//
//   printf("ret = %d %s\n",ret,(memcmp(buf,plain_msg_buf,sizeof(plain_msg_buf))==0?"same":"diff"));
//   log_hex(buf,sizeof(plain_msg_buf));printf("\n");
//   log_hex(plain_msg_buf,sizeof(plain_msg_buf));
//
//}
